<?php

/**
 * Contestant actions.
 *
 * @package    wills
 * @subpackage Contestant
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ContestantActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	  $this->area = Doctrine::getTable('Area')->findOneBySlug($request->getParameter('area'));//获得当前赛区
	  $this->areaslug=$request->getParameter('area');
    $area = Doctrine::getTable('Area')
      ->findOneBySlug($request->getParameter('area'));
  
    $this->pager = new sfDoctrinePager(
      'Contestant',
      sfConfig::get('app_per_page_contestant')
    );

	// 根据不同的筛选方式确定分页参数
	if($request->hasParameter('sort') )
	  {
		$this->whichpager='contestantSortPager';
		$this->order=$order=$request->getParameter('sort');
	  }
	else
	  {
		$this->whichpager='Pager';
		$this->order=$order=$request->getParameter('sort');
	
	  }

    $this->pager->setQuery($area->getContestantsQuery($order));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->contestants = $this->pager->getResults();
    
    $this->form = new PollLogForm();
  }

  public function executeShow(sfWebRequest $request)
  {
	$this->area = Doctrine::getTable('Area')->findOneBySlug($request->getParameter('area'));//获得当前赛区
    $this->contestant = Doctrine::getTable('contestant')
      ->find(array($request->getParameter('id')));
      
    $this->form = new PollLogForm3();
      
    $this->forward404Unless($this->contestant);

    $this->messages = $this->contestant
      ->getMessagesWithAuthorQuery()
      ->limit(5)
      ->execute();

    $this->form2 = MessageFormFactory::create();

    $this->vcrs = $this->contestant->getVcrs();
  }
  
  public function executePoll(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $this->form = new PollLogForm();
    $this->form->bind($request->getParameter('poll_log'));
    
    if ($this->form->isValid())
    {
      $this->getUser()->setFlash('message', '投票成功！');
      $this->redirect('@contestant');
    }
	else
	{
      $this->getUser()->setFlash('message', '一天只能投票一次！您今天已经投过票了。');
	  $this->redirect('@contestant');
	}
  }
  
  public function executeShowPoll(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $this->form = new PollLogForm3();
    $this->form->bind($request->getParameter('poll_log'));
    
    if ($this->form->isValid())
    {
      $this->getUser()->setFlash('message', '投票成功！');
      $this->redirect('@contestant_show?id=' . $request->getParameter('id'));
    }
    else
	{
      $this->getUser()->setFlash('message', '一天只能投票一次！您今天已经投过票了。');
	  $this->redirect('@contestant_show?id=' . $request->getParameter('id'));
	}
    //$this->setTemplate('poll');
    //$this->redirect('@contestant_show?id=' . $request->getParameter('id'));
  }

//会员中心会员查看留言
  public function executeShowmessage(sfWebRequest $request)
  {
	$this->areas = Doctrine::getTable('Area')->getHomepageList(); //获得当前赛区
    $this->contestant = Doctrine::getTable('contestant')
      ->find(array($request->getParameter('id')));
      
    $this->form = new PollLogForm3();
      
    $this->forward404Unless($this->contestant);

    $this->messages = $this->contestant
      ->getMessagesWithAuthorQuery()
      ->limit(5)
      ->execute();

    $this->form2 = MessageFormFactory::create();

    $this->vcrs = $this->contestant->getVcrs();
  }

//查看会员全部留言 
  public function executeAllmessage(sfWebRequest $request)
  {
	  $this->cid=$request->getParameter('cid');
	$this->contestant =$contestant= Doctrine::getTable('contestant')
       ->find(array($request->getParameter('cid')));
	
    $this->pager = new sfDoctrinePager(
      'Message',
      sfConfig::get('app_per_page_contestantallmessages')
    );

    $this->pager->setQuery($contestant->getMessagesWithAuthorQuery());
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->messages = $this->pager->getResults();
      
	$this->form2 = MessageFormFactory::create();

  }
// search start
 public function executeSearch(sfWebRequest $request)
 {
	 $this->query=$query = $request->getParameter('query');
	 $this->area = Doctrine::getTable('Area')->findOneBySlug($request->getParameter('area'));
	 $this->areaslug=$request->getParameter('area');
	 $this->contestants =Doctrine_Query::create()
		->from('Contestant')
		->addwhere('number <> 0')
		//->addWhere('win = ?', true)
		->addWhere('name LIKE ?', '%'.$query.'%')
		->execute();
 }	 
// search end
}
